package com.gzdh.tang.modu.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzdh.tang.dishrecognition.model.PayModelReq;
import com.gzdh.tang.dishrecognition.model.WeekMenuBakModelReq;
import com.gzdh.tang.modu.entity.*;
import com.gzdh.tang.modu.mapper.MacMainpOrderMapper;
import com.gzdh.tang.modu.model.MainpOrderPayModel;
import com.gzdh.tang.modu.service.MacMainpOrderService;
import com.gzdh.tang.modu.service.ReserveService;
import com.gzdh.tang.modu.service.ReserveXfjTempService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@DS("card")
@Service("macMainpOrderService")
public class MacMainpOrderServiceImpl extends ServiceImpl<MacMainpOrderMapper, MacMainpOrder> implements MacMainpOrderService {

    @Autowired
    ReserveXfjTempService reserveXfjTempService;

    @Autowired
    ReserveService reserveService;


    @Override
    @Transactional
    public MainpOrderPayModel saveOrderTemp(PayModelReq payModelReq, String orders, MacMainpSbruan macMainpSbruan, Fk fk) throws RuntimeException {
        MainpOrderPayModel mainpOrderPayModel = new MainpOrderPayModel();
        Date date = payModelReq.getCzrq() != null ? payModelReq.getCzrq() : new Date();
        int i = 0;
        BigDecimal amountPrice = new BigDecimal(0);
        BigDecimal amountCounts = new BigDecimal(0);
        try {

            for (WeekMenuBakModelReq goo : payModelReq.getProductCartModelList()) {
                i++;
                if ("xjc".equals(goo.getBm())) {
                    goo.setBm(macMainpSbruan.getCode());
                    goo.setZl(macMainpSbruan.getZl());
                }

                ReserveXfjTemp reserve = new ReserveXfjTemp();
                reserve.setCardno(fk.getCardno());
                reserve.setZl(goo.getZl());
                reserve.setBqh(macMainpSbruan.getBqh());
                reserve.setKsh(macMainpSbruan.getKsh());
                reserve.setBed("0");
                reserve.setPosnum(macMainpSbruan.getPos());
                reserve.setDinner(goo.getDinner());
                reserve.setCode(goo.getBm());
                reserve.setSl(goo.getNumber());
                amountPrice = amountPrice.add(new BigDecimal(goo.getNumber()).multiply(new BigDecimal(goo.getPrice())));
                amountCounts = amountCounts.add(new BigDecimal(goo.getNumber()));
                reserve.setJe(new BigDecimal(goo.getNumber()).multiply(new BigDecimal(goo.getPrice())).toPlainString());
                reserve.setYe("0");
                reserve.setKl(fk.getKl());
                reserve.setSc(macMainpSbruan.getSc() != null ? macMainpSbruan.getSc() : "3");
                reserve.setLsh2(orders);
                reserve.setCzrq(new Date(date.getTime() + (i * 10)));
                reserve.setDcrq(new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(new Date()));
                reserve.setHname(goo.getName());
                reserve.setSddz(payModelReq.getDeviceId());
                reserve.setCs(0);
                reserve.setKehu(fk.getName());
                reserve.setZfserialnumber(orders);
//                reserve.setOriginalAmount(goo.getOriginalAmount());
                reserve.setStatus("1");
                reserve.setAddr(macMainpSbruan.getAddr());
                reserve.setOriginalAmount(new BigDecimal(goo.getNumber()).multiply(new BigDecimal(goo.getPrice())).toPlainString());
                reserve.setDiscountAmount("0");
                reserve.setZkl("100");
                reserveXfjTempService.save(reserve);
            }
            MacMainpOrder macMainpOrder = new MacMainpOrder();
            macMainpOrder.setOrders(orders);
            macMainpOrder.setOperaId(payModelReq.getUserId());
            macMainpOrder.setPosNum(macMainpSbruan.getPos());
            macMainpOrder.setPayChannel(1);
            macMainpOrder.setQcCode("");
            macMainpOrder.setPayState(0);
            macMainpOrder.setAmountCount(amountCounts.intValue());
            macMainpOrder.setNoticeReserve(0);
            macMainpOrder.setAddr(macMainpSbruan.getAddr());
            macMainpOrder.setAmountPrice(amountPrice.toPlainString());
            macMainpOrder.setOrderType(0);
            macMainpOrder.setRefundOrder("");
            macMainpOrder.setRefundMsg("");
            macMainpOrder.setRefundState(0);
            macMainpOrder.setCreateTime(new Date());
            this.save(macMainpOrder);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("异常:" + e.toString());
            throw new RuntimeException("数据库写入失败");
        }
        mainpOrderPayModel.setAmountCounts(amountCounts);
        mainpOrderPayModel.setAmountPrice(amountPrice);
        return mainpOrderPayModel;
    }

    @Transactional
    @Override
    public boolean judOrder(String orders) {
        List<ReserveXfjTemp> reserveXfjTempList = reserveXfjTempService
                .list(new QueryWrapper<ReserveXfjTemp>().lambda().eq(ReserveXfjTemp::getZfserialnumber, orders));
        List<Reserve> reserveList = new ArrayList<>();
        for (ReserveXfjTemp goo : reserveXfjTempList) {
            Reserve reserve = new Reserve();
            BeanUtils.copyProperties(goo, reserve);
            reserveList.add(reserve);
        }
        reserveService.saveBatch(reserveList);
        this.lambdaUpdate().set(MacMainpOrder::getPayState, 1)
                .set(MacMainpOrder::getNoticeReserve, 1)
                .eq(MacMainpOrder::getOrders, orders).update();
        return false;
    }

    @Override
    public boolean judOrder(String orders, String transactionId) {
        List<ReserveXfjTemp> reserveXfjTempList = reserveXfjTempService
                .list(new QueryWrapper<ReserveXfjTemp>().lambda().eq(ReserveXfjTemp::getZfserialnumber, orders));
        List<Reserve> reserveList = new ArrayList<>();
        for (ReserveXfjTemp goo : reserveXfjTempList) {
            Reserve reserve = new Reserve();
            BeanUtils.copyProperties(goo, reserve);
            reserve.setTransactionId(transactionId);
            reserveList.add(reserve);
        }
        reserveService.saveBatch(reserveList);
        this.lambdaUpdate().set(MacMainpOrder::getPayState, 1)
                .set(MacMainpOrder::getNoticeReserve, 1)
                .eq(MacMainpOrder::getOrders, orders).update();
        return false;
    }
}
